package com.dongdongshop.service.imp;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.pojo.Cart;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.service.CartService;
import com.dongdongshop.service.GoodsItemService;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@org.springframework.stereotype.Service
public class CartServiceImpl implements CartService {
    @Reference
    private GoodsItemService goodsItemService;
    @Reference
    private GoodsService goodsService;
    @Reference
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Cart> addCart(List<Cart> carts, Long itemId, Integer num) {
        //三种情况
        //1购物车没数据或者购物车有数据但没有当前店铺的数据
        //2有数据,也有当前店铺的东西,但没有这件商品
        //3有数据,也有店铺的东西,也买过这件商品


        //4根据itemid查询商品信息
        TbItem tbItem = goodsItemService.findItemById(itemId);
        //5拿到商家的id
        String sellerId = tbItem.getSellerId();
        //6根据sellerId判断carlist之前有没有买过这家店铺的东西
        Cart cart = getCartBySellerId(carts,sellerId);
        if (cart == null){
            //7.1没有买过
            //7.2创建购物车对象
            //7.3将数据放到购物车中
            cart = new Cart();
            cart.setSellerId(sellerId);
            cart.setSellerName(tbItem.getSeller());
            List<TbOrderItem> list = new ArrayList<>();
            TbOrderItem orderItem = new TbOrderItem();
            orderItem.setGoodsId(tbItem.getGoodsId());
            orderItem.setItemId(itemId);
            orderItem.setNum(num);
            orderItem.setTitle(tbItem.getTitle());
            orderItem.setPrice(tbItem.getPrice());
            orderItem.setPicPath(tbItem.getImage());
            orderItem.setSellerId(sellerId);
            orderItem.setTotalFee(new BigDecimal(tbItem.getPrice().doubleValue()*num));
            orderItem.setGoodsId(tbItem.getGoodsId());
            list.add(orderItem);
            cart.setList(list);
            carts.add(cart);
        }else{
            //8.1如果买过,也就是sellerId不为null
            //8.2购物车明细表中是否由该商品
            List<TbOrderItem> orderItem = cart.getList();
            TbOrderItem tbOrderItem = getCartByItemId(orderItem, itemId);
            if (tbOrderItem == null){
                tbOrderItem = new TbOrderItem();
                tbOrderItem.setGoodsId(tbItem.getGoodsId());
                tbOrderItem.setItemId(itemId);
                tbOrderItem.setNum(num);
                tbOrderItem.setTitle(tbItem.getTitle());
                tbOrderItem.setPrice(tbItem.getPrice());
                tbOrderItem.setPicPath(tbItem.getImage());
                tbOrderItem.setSellerId(sellerId);
                tbOrderItem.setTotalFee(new BigDecimal(tbItem.getPrice().doubleValue()*num));
                tbOrderItem.setGoodsId(tbItem.getGoodsId());
                orderItem.add(tbOrderItem);
            }else{ //8.3如果没有该商品,list添加,如果有,修改数量和总价格
                tbOrderItem.setNum(tbOrderItem.getNum()+num);
                tbOrderItem.setTotalFee(new BigDecimal(tbItem.getPrice().doubleValue()*tbOrderItem.getNum()));
                if (tbOrderItem.getNum() <= 0){
                    orderItem.remove(tbOrderItem);
                }
                if (orderItem.size() <= 0){
                    carts.remove(cart);
                }
            }
        }
        return carts;
    }


    //判断购物车中有没有改店铺的东西
    public Cart getCartBySellerId(List<Cart> carts,String sellerId){
        for (Cart cart : carts) {
            if (Objects.equals(cart.getSellerId(),sellerId)){
                return cart;
            }
        }
        return null;
    }

    //购物车中有该店铺,判断购物车中有没有该商品
    public TbOrderItem getCartByItemId(List<TbOrderItem> items, Long itemId){
        for (TbOrderItem item : items) {
            if (Objects.equals(item.getItemId(),itemId)){
                return item;
            }
        }
        return null;
    }

    @Override
    public List<Cart> findCartListFromRedis(String userId) {
        String s = (String) redisTemplate.opsForValue().get(userId);
        List<Cart> carlist = JSONObject.parseArray(s,Cart.class);
        if (carlist == null){
            carlist = new ArrayList<>();
        }
        return carlist;
    }

    @Override
    public void saveCartListRedis(String userId, List<Cart> cartlist) {
        String str = JSONObject.toJSONString(cartlist);
        redisTemplate.opsForValue().set(userId,str);
    }

    @Override
    public List<Cart> mergeCartListCookieAndReids(List<Cart> carts, List<Cart> list) {
        for (Cart cartcookie : carts) {
            for (TbOrderItem tbOrderItem : cartcookie.getList()) {
                list = addCart(list,tbOrderItem.getItemId(),tbOrderItem.getNum());
            }
        }
        return list;
    }
}
